/**
 * waterQuality vuex
 */
import posts from '@/plugins/request'
// initial state
const state = {
  assetAdminList: [],
  stationManagers: [],
  stationConsumable: [],
  carAdmin: {
    data: [],
    total: 0
  }
}
// getters
const getters = {
  getAssetAdminList: state => state.assetAdminList,
  getStationManagers: state => state.stationManagers,
  getStationConsumable: state => state.stationConsumable,
  getCarAdminList: state => state.carAdmin
}
// actions

const actions = {
  async fetchAssetAdminList({ commit }, param) {
    let data = await posts.fetchAssetAdminList(param)

    
    let resData = []
    for(let i in data) {
      resData.push({
        stationName: i,
        airConditioningCount: data[i].airConditioningCount[0] ? data[i].airConditioningCount[0].storageCount : 0,
        controlSystemCount: data[i].controlSystemCount[0] ? data[i].controlSystemCount[0].storageCount : 0,
        distributionSystemCount: data[i].distributionSystemCount[0] ? data[i].distributionSystemCount[0].storageCount : 0,
        instrumentationCount: data[i].instrumentationCount[0] ? data[i].instrumentationCount[0].storageCount : 0,
        pretreatmentSystemCount: data[i].pretreatmentSystemCount[0] ? data[i].pretreatmentSystemCount[0].storageCount : 0,
        consumableCount: data[i].consumableCount
      })
    }
    commit('changeAssetAdminList', resData)
  },
  async fetchAssetStationManager({ commit }) {
    let data = await posts.fetchAssetStationManager()
    commit('changeStationManagers', data)
  },
  async fetchAssetConsumable({ commit }) {
    let data = await posts.fetchAssetConsumable()
    commit('changeStationConsumable', data)
  },
  async postAssetAdminEntry({ commit }, param) {
    let result = await posts.postAssetAdminEntry(param)
    return result
  },
  async fetchCarAdminList({ commit }, param) {
    let data = await posts.fetchCarAdminList(param)
    commit('changeCarAdminList', data)
  },
  async postCarAdminAdd({ commit }, param) {
    let result = await posts.postCarAdminAdd(param)
    return result
  },
}

// mutations
const mutations = {
  changeAssetAdminList(state, data) {
    state.assetAdminList = data
  },
  changeStationManagers(state, data) {
    state.stationManagers = data
  },
  changeStationConsumable(state, data) {
    state.stationConsumable = data
  },
  changeCarAdminList(state, data) {
    state.carAdmin.data = data.data
    state.carAdmin.total = data.total
  },
}


export default {
  state,
  getters,
  actions,
  mutations
}